﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MH.POE.ClipBoard.Properties;

namespace MH.POE.ClipBoard
{
    public partial class ClipBoardForm : Form
    {
        private Timer _ts;
        private const String _DefaultText = "No PoE Text Data";
        private const String _ItemLevel = "Itemlevel";
        private String _LastText = String.Empty;
        public ClipBoardForm()
        {
            InitializeComponent();
            this.InitSettings();
        }

        private void InitSettings()
        {
            this.ShowText(ClipBoardForm._DefaultText);
            this.InitTimer();
            this.InitPosition();
        }

        private void InitPosition()
        {
            //default Wert der denk ich nie in Frage kommt
            if (Settings.Default.LastPosition.X != 15000 && Settings.Default.LastPosition.Y != -Settings.Default.LastPosition.X)
            {
                this.StartPosition = FormStartPosition.Manual;
                this.SetDesktopLocation(Settings.Default.LastPosition.X, Settings.Default.LastPosition.Y);
            }
            this.Move += new EventHandler(OnEventClipBoardForm_Move);
        }

        void OnEventClipBoardForm_Move(object sender, EventArgs e)
        {
            Settings.Default.LastPosition = this.DesktopLocation;
        }

        public void InitTimer()
        {
            this._ts = new Timer();
            this._ts.Interval = 1000; //1x pro Sekunde prüfen
            this._ts.Tick += new EventHandler(this.OnEventTimerTick);
            this._ts.Start();
        }

        void OnEventTimerTick(object sender, EventArgs e)
        {
            this.GetClipBoardData();
        }

        private void GetClipBoardData()
        {
            if (Clipboard.ContainsText())
            {
                String text = Clipboard.GetText();
                if (text != this._LastText)
                {
                    this._LastText = text;
                    this.ShowText(this.FormatText(text));
                }
            }
        }

        private String FormatText(String text)
        {
            String t = text;
            if (t.Contains(ClipBoardForm._ItemLevel))
            {
                var tData = from i in t.Split(Environment.NewLine.ToCharArray())
                            where !String.IsNullOrEmpty(i)
                            select i;
                var tNew = String.Empty;
                // die 2. und 3. Zeile beeinhaltet den Name / Typ:
                if (tData.Count() > 3)
                {
                    tNew = String.Format("{0} - {1}{2}", tData.ElementAt(1), tData.ElementAt(2), Environment.NewLine);
                    foreach (var line in tData)
                    {
                        if (line.Contains(ClipBoardForm._ItemLevel))
                            tNew = String.Format("{0}{1}", tNew, line);
                    }
                    t = tNew;
                }
            }
            else
                t = ClipBoardForm._DefaultText;
            return t;
        }

        private void ShowText(string text)
        {
            this.label1.Text = text;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            this._ts.Stop();
            Settings.Default.Save();

            base.OnClosing(e);
        }

    }
}
